import { Toast } from '@/utils/utils'
import {
	getDecryptPhone,
	wxUserLogin,
	getFirstClass,
	getProducts,
	getContacts,
	getProductById,
	searchProductByKeyWord,
	getFavourites,
	addFavourites,
	deleteFavourites,
	generateOrder,
	addUserAdress,
	getUserAdress,
	updateAdress,
	deleteAdress,
	orderPay,
	getAllOrders,
	deleteOrders,
	confirmOrder,
	readSwiper
} from '@/api/httpservices'


/**
 * @description 登录
 */
export async function getPhoneNumber(e:any,loginCode:any,callBack:(e:any)=>void){
	//console.log("是否同意获取手机号码:",e);
	uni.showLoading({
		title:"登录中"
	})
	if (e.detail.errMsg == "getPhoneNumber:ok") {
		setTimeout(()=>{ uni.hideLoading() },2000);
		//Toast("同意获取手机号码");
		let iv:string=e.detail.iv;
		let encryptedData:string=e.detail.encryptedData;
		//先登录获取用户session_key
		let loginRes:any=await wxUserLogin("/wxLogin",{logincode:loginCode.value});
		//console.log("登录结果",loginRes)
		if(loginRes.data.code===200 && loginRes.data.data.token!=null && loginRes.data.data.token!=""){
			//Toast("登录成功");
			
			//设置token否则后续无法操作
			//在apiFunction.ts中获取
			uni.setStorageSync("token",loginRes.data.data.token);
			
			//防止token未生成
			//setTimeout(async ()=>{
				//之后获取用户手机号
				let paraData:wxDecryptPhone={
					token:uni.getStorageSync("token"),
					iv:iv,
					encryptedData:encryptedData,	
				}
				//paraData.token=null;
				//console.log("上传的参数",paraData)
				let res:any=await getDecryptPhone("/wxDecryptPhone",paraData);
				//console.log('服务器返回res: ',res);
				//获取到手机成功后，表示登录成功，永久化保存数据
				if(res.data.code===200){
					if(callBack instanceof Function) return callBack(res); 
				}else{
					Toast(res.data.msg);
				}
			//},2000)	
		}else{
				Toast(loginRes.data.msg);
		}
	}else{
		setTimeout(()=>{ uni.hideLoading() },5000);
	}
}


/**
 * @description 获取主类
 */
export function getFirst() {
	return getFirstClass("/firsttype/getFirst");
}



/**
 * @description 获取产品信息
 */
export async function getAllProducts() {
	return getProducts("/products/getProducts");
}



/**
 * @description 获取联系方式
 */
export function getContact() {
	return getContacts("/contacts/getContacts");
}



/**
 * @description 获取单个产品信息
 */
export function getProductInfoById(productId:number) {
	return getProductById("/products/getProductById/"+productId);
}


/**
 * @description 搜索商品信息
 */
export function searchProducts(keyWord:string) {
	return searchProductByKeyWord("/products/searchProductByKeyWord/"+keyWord);
}


/**
 * @description 获取用户收藏数据
 */
export function getUserFavourites(userid:number) {
	return getFavourites("/favourites/getFavourites/"+userid);
}



/**
 * @description 删除收藏数据
 * @param  { number } favId 收藏数据id
 */
export function deleteFavouritesData(favId:number) {
	return deleteFavourites("/favourites/deleteFavourites/"+favId);
}




/**
 * @description 收藏
 * @param  { number } userId 用户id
 * @param  { number } productId 产品id
 */
export function addFavouriteData(userId:number,productId:number) {
	console.log("收藏的id",userId,productId);
	return addFavourites("/favourites/addFavourites/"+userId+"/"+productId);
}




/**
 * @description 生成订单数据
 */
export function generateOrderData(data:any) {
	return generateOrder("/orders/generateOrder/",data);
}



/**
 * @description 添加收货地址
 */
export function addUserAdressData(data:any) {
	return addUserAdress("/address/addUserAdress/",data);
}


/**
 * @description 获取用户收货地址
 */
export function getUserAdressData(data:any) {
	return getUserAdress("/address/getUserAdress/",data);
}

/**
 * @description 更新收货地址
 */
export function updateAdressData(data:any) {
	return updateAdress("/address/updateAdress/",data);
}



/**
 * @description 删除收货地址
 */
export function deleteAdressData(data:any) {
	return deleteAdress("/address/deleteAdress/",data);
}


/**
 * @description 统一下单支付
 */
export function orderPayData(data:any) {
	return orderPay("/orders/orderPay/",data);
}



/**
 * @description 读取所有下单信息
 */
export function getAllOrdersData(data:any) {
	return getAllOrders("/orders/getAllOrders/",data);
}

/**
 * @description 删除订单
 */
export function deleteOrdersData(data:any) {
	return deleteOrders("/orders/deleteOrder/",data);
}

/**
 * @description 确认订单
 */
export function confirmOrderData(data:any) {
	return confirmOrder("/orders/confirmOrder/",data);
}


/**
 * @description 读取轮播图
 */
export function readSwiperData(data:any) {
	return readSwiper("/readSwiper",data);
}





